﻿using System;
using System.Collections.Generic;
using System.Web;


using RexToy.Template;

namespace RexToy.UI.Web.Tags
{
    class WebTagContext : ITemplateContext
    {
        private Dictionary<string, object> _tables;
        public WebTagContext()
        {
            _tables = new Dictionary<string, object>();
        }

        public void AddVar(string name)
        {
            _tables.Add(name, null);
        }

        public bool ExistVar(string name)
        {
            if (_tables.ContainsKey(name))
                return true;

            if (HttpContext.Current.Items.Contains(name))
                return true;

            string s = HttpContext.Current.Request.Url.ToString();
            //Extend: Search Session?
            //foreach (var key in HttpContext.Current.Session.Keys)
            //{
            //    string k = key as string;
            //    if (k == null)
            //        continue;
            //    if (k == name)
            //        return true;
            //}

            return false;
        }

        public void RemoveVar(string name)
        {
            _tables.Remove(name);
        }

        public void Assign(string param, object value)
        {
            _tables[param] = value;
        }

        public object Resolve(string param)
        {
            object result;
            if (_tables.TryGetValue(param, out result))
                return result;

            if (HttpContext.Current.Items.Contains(param))
                return HttpContext.Current.Items[param];

            return result;
        }
    }
}
